<?php
class LanguagesController extends Controller
{
	public $page_title = LANG_LANGUAGES;
	public $layout = 'main';
	public $module = 'administration';
	public $helpers = array ('html', 'htmlx', 'toolbars', 'grids|index');
	public $name = 'languages';
	public $use_logging = false;

	function Index()
	{
		$this->data[$this->name] = $this->controller_model->ListItems();
	}	
	
	/**
	 * redirects to the edit page
	 *
	 * @param string $lang language
	 */
	function ViewTranslation($lang)
	{
		$this->Redirect("languages/edittranslation/$lang");
	}
	
	/**
	 * edits translations for the given language
	 *
	 * @param string $lang language
	 */
	function EditTranslation($lang, $file = 'text')
	{
		$this->data['lang'] = $lang;
		$this->data['file'] = $file;
		
		if ($_POST['submitTrans'] == 'submit')
		{
			//make copy and rewrite the file
			copy(LANGUAGES.SEPARATOR.$this->data[lang].SEPARATOR.$file.'.php',LANGUAGES.SEPARATOR.$this->data[lang].SEPARATOR.$file.'_backup.php');
			$fp = fopen(LANGUAGES.SEPARATOR.$this->data[lang].SEPARATOR.$file.'.php', 'w+');
			fwrite($fp, "<?php \n");
			while ($elem = each($_POST))
			{
				$output = "define('".$elem["key"]."','".$elem["value"]."');\n";
				fwrite($fp, $output);
			}
			fwrite($fp, "?>");
			fclose($fp);
			$out.='<br/>'.LANG_TRANSLATION_UPDATED.'<br />';		
		}		
		
		//reads the file
		$trans_line = file(LANGUAGES.SEPARATOR.$this->data[lang].SEPARATOR.$file.'.php');
		$trans_num = count($trans_line);
		for ($i=1; $i<$trans_num; $i++)
		{
			$trans = explode("','", $trans_line[$i]);
			$trans = str_replace("define('", "", $trans);
			$trans = str_replace("');", "", $trans);
			if (!ereg("^[A-Z]" ,$trans['0'])) {continue;}
			$tr[$i]['0']=$trans['0'];
			$tr[$i]['1']=$trans['1'];
		}
		$this->data['trans_num'] = $trans_num;
		$this->data['tr'] = $tr;			
		$this->data['out'] = $out;			
	}
}
?>